#pragma once
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

namespace wjb
{
	template<typename T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _val;

		list_node(const T& val = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _val(val)
		{}

	};

	template<typename T, typename Ref, typename Ptr>
	struct list_iterator
	{
		typedef list_node<T> node;
		typedef list_iterator<T, Ref, Ptr> self;
		node* _node;
		list_iterator(node* n)
			:_node(n)
		{}

		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self operator++(int)
		{
			_node = _node->_next;
			return _node->_prev;
		}
		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		self operator--(int)
		{
			_node = _node->_prev;
			return _node->_prev;
		}

		Ref operator*()
		{
			return _node->_val;
		}

		Ptr operator->()
		{
			return &(_node->_val);
		}

		bool operator!=(const self& it)const
		{
			return _node != it._node;
		}

		bool operator==(const self& it)const
		{
			return _node == it._node;
		}


	};

	template<typename T>
	class list
	{
		typedef list_node<T> node;
	public:
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;
		typedef reverse_iterator< iterator, T&, T*> reverse_iterator;

		list()
		{
			list_init();
		}

		void list_init()
		{
			_head = new node;
			_head->_prev = _head;
			_head->_next = _head;
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		list(const list<T>& lt)
		{
			list_init();
			const_iterator it = lt.cbegin();
			while (it != lt.cend())
			{
				push_back(*it);
				++it;
			}
		}

		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		void push_back(const T& val = T())
		{
			/*node* tail = _head->_prev;
			node* newnode = new node(val);
			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;*/
			insert(end(), val);
		}
		void pop_back()
		{
			erase(--end());
		}

		void push_front(const T& val)
		{
			insert(begin(), val);
		}
		void pop_front()
		{
			erase(begin());
		}


		iterator begin()
		{
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}
		const_iterator cbegin()const
		{
			return _head->_next;
		}
		const_iterator cend()const
		{
			return _head;
		}

		reverse_iterator rbegin()
		{
			return end();
		}

		reverse_iterator rend()
		{
			return begin();
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());
			node* ret = pos._node->_next;
			pos._node->_prev->_next = pos._node->_next;
			pos._node->_next->_prev = pos._node->_prev;
			delete pos._node;
			_size--;
			return ret;
		}

		void insert(iterator pos, const T& val = T())
		{
			node* newnode = new node(val);
			node* cur = pos._node;
			node* prev = cur->_prev;
			cur->_prev = newnode;
			prev->_next = newnode;
			newnode->_next = cur;
			newnode->_prev = prev;
			_size++;
		}

		size_t size()const
		{
			return _size;
		}
		bool empty()const
		{
			return _size == 0;
		}

	private:
		node* _head;
		size_t _size;
	};
}